#ifndef CANVASHANDLER_H
#define CANVASHANDLER_H

#include <QObject>
#include <QCursor>
#include <QMap>
#include "CanvasToolBarModel.h"
#include "SerializeManager.h"
#include <QMutex>
#include <QMutexLocker>
#include "tcpblock.h"
using namespace PROTO;


class CanvasHandler : public QObject
{
    Q_OBJECT
public:
    CanvasHandler(QObject *parent = nullptr);

    static CanvasHandler* instance();

    Q_INVOKABLE void canvasClear();

    Qt::CursorShape cursorShape() const;
    ToolType currentToolType() const;
    Q_INVOKABLE void sendFigure(const ToolType type, const int x, const int y, const int w, const int h);

    Q_INVOKABLE void recover();       // 恢复Canvas
    Q_INVOKABLE void canvasBackout(); // 撤销

    Q_INVOKABLE void saveToFile(QString path);  // 保存为文件
    Q_INVOKABLE void loadFromFile(QString path);    // 从文件中加载

    void pushFigure(Shape& sp);      // 图形入栈

    void setCursorShape(const Qt::CursorShape cursorShape);
    void setCurrentToolType(const ToolType& type);

signals:
    void cursorShapeChanged();
    void currentToolTypeChanged();
    void clearCanvas();
    void draw(QVariant type, QVariant x, QVariant y, QVariant h, QVariant w);
    void notice(QVariant str);

private:
    void registerFunctor();
    void handleNewBlock(qint64 magic, QByteArray data);

    void recoverCanvas(); // 恢复Canvas状态

private:
    ToolType        m_currentToolType;
    Qt::CursorShape m_cursorShape;

    QVector<Shape> stk; // 操作栈
    QMutex m_mutex;

    Q_PROPERTY(Qt::CursorShape cursorShape READ cursorShape WRITE setCursorShape NOTIFY cursorShapeChanged)
    Q_PROPERTY(ToolType currentToolType READ currentToolType WRITE setCurrentToolType NOTIFY currentToolTypeChanged)
};


#endif // CANVASHANDLER_H
